﻿#include "csvworker.h"
#include <QDateTime>
#include <QTextStream>
#include <QTextCodec>
CsvWorker::CsvWorker(QObject *parent) : QObject(parent)
{

}
void CsvWorker::slotWriteIn(CsvData m_csData)
{
	// 1.创建文件  根据时间进行命名
	//2.文件名采用系统时间戳生成唯一的文件
	m_nIndex++;
	m_csData.csIndex = QString::number(m_nIndex);
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString current_date = QString("%1/%2/%3").arg("data").arg(current_date_time.toString("yyyy_MM")).arg(current_date_time.toString("yyyy_MM_dd"));
	QString csvDir = tr("/%1_%2_export_%3.csv").arg(current_date_time.toString("yyyy_MM_dd hh-mm-ss")).arg(m_csData.csName).arg(m_csData.csIndex);
	CreateCsvFile(current_date, csvDir);
	// 2.打开文件
	QFile data(csvFile);
	// 3.进行写入
	if (data.open(QFile::ReadWrite | QFile::Text | QIODevice::Append))        // 打开文件
	{
		//检测开始时间,检测结束时间,产品类型,产品批次,产品名,产品规格,初始数量,结束数量,良品数,良品率,不良数,不良率,尺寸不合格数,缺陷不合格数,混料,异常
		QTextStream out(&data);    // 输入流

		out << m_csData.csStarTime << "," << m_csData.csStopTime << "," << m_csData.csType
			<< "," << m_csData.csIndex << "," << m_csData.csName << "," << m_csData.csSize
			<< "," << m_csData.csStarNum << "," << m_csData.csStopNum << "," << m_csData.csGodNum
			<< "," << m_csData.csGodRatio << "," << m_csData.csBadNum << "," << m_csData.csBadRatio
			<< "," << m_csData.csNG1 << "," << m_csData.csNG2 << "," << m_csData.csNG3 << "," << m_csData.csNG4 << ",";
		switch (m_nType) {
		case 0:
			// 长方形
            // 宽度  长度   厚度 角度1 ~ 4
            out << m_csData.csNum1 << "," << m_csData.csNum2 << ","
                << m_csData.csNum3 << "," << m_csData.csNum4 << ","
                << m_csData.csNum5 << "," << m_csData.csNum6 << ",";

			out << m_csData.csNum1Up << "," << m_csData.csNum1UpRatio << "," << m_csData.csNum1Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum2Up << "," << m_csData.csNum2UpRatio << "," << m_csData.csNum2Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum3Up << "," << m_csData.csNum3UpRatio << "," << m_csData.csNum3Down
				<< "," << m_csData.csNum3DownRatio << "," << m_csData.csNum3Unknown << ",";

//            strData += QString::fromLocal8Bit("角度1 OK,角度1 NG,角度2 OK,角度2 NG,角度3 OK,角度3 NG,角度4 OK,角度4 NG,");
//            strData += QString::fromLocal8Bit("角度1上限,角度1上限百分比,角度1下限,角度1下限百分比,角度1不确定,");
//            strData += QString::fromLocal8Bit("角度2上限,角度2上限百分比,角度2下限,角度2下限百分比,角度2不确定,");
//            strData += QString::fromLocal8Bit("角度3上限,角度3上限百分比,角度3下限,角度3下限百分比,角度3不确定,");
//            strData += QString::fromLocal8Bit("角度4上限,角度4上限百分比,角度4下限,角度4下限百分比,角度4不确定");
            out << m_csData.csAngle1OK << "," << m_csData.csAngle1NG << ","
                << m_csData.csAngle2OK << "," << m_csData.csAngle2NG << ","
                << m_csData.csAngle3OK << "," << m_csData.csAngle3NG << ","
                << m_csData.csAngle4OK << "," << m_csData.csAngle4NG << ",";

            out << m_csData.csAngle1Up << "," << m_csData.csAngle1UpRatio << ","
                << m_csData.csAngle1Down << "," << m_csData.csAngle1DownRatio << ","
                << m_csData.csAngle1Unknown << ",";
            out << m_csData.csAngle2Up << "," << m_csData.csAngle2UpRatio << ","
                << m_csData.csAngle2Down << "," << m_csData.csAngle2DownRatio << ","
                << m_csData.csAngle2Unknown << ",";
            out << m_csData.csAngle3Up << "," << m_csData.csAngle3UpRatio << ","
                << m_csData.csAngle3Down << "," << m_csData.csAngle3DownRatio << ","
                << m_csData.csAngle3Unknown << ",";
            out << m_csData.csAngle4Up << "," << m_csData.csAngle4UpRatio << ","
                << m_csData.csAngle4Down << "," << m_csData.csAngle4DownRatio << ","
                << m_csData.csAngle4Unknown << ",";



			out << "\n";
			break;
		case 1:
			// 正方形
            // 宽度  长度   厚度 角度1 ~ 4
			out << m_csData.csNum1 << "," << m_csData.csNum2 << "," << m_csData.csNum3
				<< "," << m_csData.csNum4 << "," << m_csData.csNum5 << "," << m_csData.csNum6 << ",";

			out << m_csData.csNum1Up << "," << m_csData.csNum1UpRatio << "," << m_csData.csNum1Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum2Up << "," << m_csData.csNum2UpRatio << "," << m_csData.csNum2Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum3Up << "," << m_csData.csNum3UpRatio << "," << m_csData.csNum3Down
				<< "," << m_csData.csNum3DownRatio << "," << m_csData.csNum3Unknown << ",";



            out << m_csData.csAngle1OK << "," << m_csData.csAngle1NG << ","
                << m_csData.csAngle2OK << "," << m_csData.csAngle2NG << ","
                << m_csData.csAngle3OK << "," << m_csData.csAngle3NG << ","
                << m_csData.csAngle4OK << "," << m_csData.csAngle4NG << ",";

            out << m_csData.csAngle1Up << "," << m_csData.csAngle1UpRatio << ","
                << m_csData.csAngle1Down << "," << m_csData.csAngle1DownRatio << ","
                << m_csData.csAngle1Unknown << ",";
            out << m_csData.csAngle2Up << "," << m_csData.csAngle2UpRatio << ","
                << m_csData.csAngle2Down << "," << m_csData.csAngle2DownRatio << ","
                << m_csData.csAngle2Unknown << ",";
            out << m_csData.csAngle3Up << "," << m_csData.csAngle3UpRatio << ","
                << m_csData.csAngle3Down << "," << m_csData.csAngle3DownRatio << ","
                << m_csData.csAngle3Unknown << ",";
            out << m_csData.csAngle4Up << "," << m_csData.csAngle4UpRatio << ","
                << m_csData.csAngle4Down << "," << m_csData.csAngle4DownRatio << ","
                << m_csData.csAngle4Unknown << ",";

			out << "\n";
			break;
		case 2:
			// 梯形
			//0 高度   2 左斜边角度   3  右斜边角度
			out << m_csData.csNum1 << "," << m_csData.csNum2 << "," << m_csData.csNum3
				<< "," << m_csData.csNum4 << "," << m_csData.csNum5 << "," << m_csData.csNum6 << ",";

			out << m_csData.csNum1Up << "," << m_csData.csNum1UpRatio << "," << m_csData.csNum1Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum2Up << "," << m_csData.csNum2UpRatio << "," << m_csData.csNum2Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum3Up << "," << m_csData.csNum3UpRatio << "," << m_csData.csNum3Down
				<< "," << m_csData.csNum3DownRatio << "," << m_csData.csNum3Unknown << ",";

			out << "\n";
			break;
		case 3:
			// 异形
			//高度  宽度 厚度
			out << m_csData.csNum1 << "," << m_csData.csNum2 << "," << m_csData.csNum3
				<< "," << m_csData.csNum4 << "," << m_csData.csNum5 << "," << m_csData.csNum6 << ",";

			out << m_csData.csNum1Up << "," << m_csData.csNum1UpRatio << "," << m_csData.csNum1Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum2Up << "," << m_csData.csNum2UpRatio << "," << m_csData.csNum2Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum3Up << "," << m_csData.csNum3UpRatio << "," << m_csData.csNum3Down
				<< "," << m_csData.csNum3DownRatio << "," << m_csData.csNum3Unknown << ",";

			out << "\n";
			break;
		case 4:
			// 扇形
			//弧边1长度  弧边2长度  直边3长度  直边4长度  中直长度 轮廓度
			out << m_csData.csNum1 << "," << m_csData.csNum2 << "," << m_csData.csNum3
				<< "," << m_csData.csNum4 << "," << m_csData.csNum5 << "," << m_csData.csNum6
				<< "," << m_csData.csNum7 << "," << m_csData.csNum8 << "," << m_csData.csNum9
				<< "," << m_csData.csNum10 << "," << m_csData.csNum11 << "," << m_csData.csNum12 << ",";

			out << m_csData.csNum1Up << "," << m_csData.csNum1UpRatio << "," << m_csData.csNum1Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum2Up << "," << m_csData.csNum2UpRatio << "," << m_csData.csNum2Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum3Up << "," << m_csData.csNum3UpRatio << "," << m_csData.csNum3Down
				<< "," << m_csData.csNum3DownRatio << "," << m_csData.csNum3Unknown << ",";
			out << m_csData.csNum4Up << "," << m_csData.csNum4UpRatio << "," << m_csData.csNum4Down
				<< "," << m_csData.csNum4DownRatio << "," << m_csData.csNum4Unknown << ",";
			out << m_csData.csNum5Up << "," << m_csData.csNum5UpRatio << "," << m_csData.csNum5Down
				<< "," << m_csData.csNum5DownRatio << "," << m_csData.csNum5Unknown << ",";
			out << m_csData.csNum6Up << "," << m_csData.csNum6UpRatio << "," << m_csData.csNum6Down
				<< "," << m_csData.csNum6DownRatio << "," << m_csData.csNum6Unknown << ",";

			out << "\n";


			break;
		case 5:
			// 子弹头
			//宽度  长度  角度1  角度2
			out << m_csData.csNum1 << "," << m_csData.csNum2 << "," << m_csData.csNum3
				<< "," << m_csData.csNum4 << "," << m_csData.csNum5 << "," << m_csData.csNum6
				<< "," << m_csData.csNum7 << "," << m_csData.csNum8 << ",";


			out << m_csData.csNum1Up << "," << m_csData.csNum1UpRatio << "," << m_csData.csNum1Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";
			out << m_csData.csNum2Up << "," << m_csData.csNum2UpRatio << "," << m_csData.csNum2Down
				<< "," << m_csData.csNum1DownRatio << "," << m_csData.csNum1Unknown << ",";

			out << "\n";

			break;
		default:
			break;
		}
	}
	// 4.关闭文件
	data.close();
}

void CsvWorker::slotReadOut()
{

}

void CsvWorker::CreateCsvFile(QString filePath, QString fileName)
{
	QDir tempDir;
	//临时保存程序当前路径
	QString currentDir = tempDir.currentPath();
	//如果filePath路径不存在，创建它
	if (!tempDir.exists(filePath))
	{
		tempDir.mkpath(filePath);
	}
	QFile tempFile(fileName);
	//将程序的执行路径设置到filePath下
	tempDir.setCurrent(filePath);
	csvFile = tempDir.currentPath() + fileName;
	//检查filePath路径下是否存在文件fileName,如果停止操作。
	if (tempFile.exists(fileName))
	{
		return;
	}
	//此时，路径下没有fileName文件，使用下面代码在当前路径下创建文件
	tempFile.setFileName(csvFile);
	//    QString strData = QString::fromLocal8Bit("检测时间,停止时间,操作者,进料总数,良品数,"
	//                                             "不良数,合格率,不合格率,相机1废品数,相机2废品数,相机3废品数,相机4废品数,");
	QString strData = QString::fromLocal8Bit("检测开始时间,检测结束时间,产品类型,产品批次,产品名,产品规格,初始数量,结束数量,良品数,良品率,不良数,不良率,尺寸不合格数,缺陷不合格数,混料,异常,");
	switch (m_nType) {
	case 0:
		// 长方形
        // 宽度  长度   厚度 角度1 ~ 4
		strData += QString::fromLocal8Bit("宽度 OK,宽度 NG,长度 OK,长度 NG,厚度 OK,厚度 NG,");
		strData += QString::fromLocal8Bit("宽度上限,宽度上限百分比,宽度下限,宽度下限百分比,宽度不确定,");
		strData += QString::fromLocal8Bit("长度上限,长度上限百分比,长度下限,长度下限百分比,长度不确定,");
        strData += QString::fromLocal8Bit("厚度上限,厚度上限百分比,厚度下限,厚度下限百分比,厚度不确定,");


        strData += QString::fromLocal8Bit("角度1 OK,角度1 NG,角度2 OK,角度2 NG,角度3 OK,角度3 NG,角度4 OK,角度4 NG,");
        strData += QString::fromLocal8Bit("角度1上限,角度1上限百分比,角度1下限,角度1下限百分比,角度1不确定,");
        strData += QString::fromLocal8Bit("角度2上限,角度2上限百分比,角度2下限,角度2下限百分比,角度2不确定,");
        strData += QString::fromLocal8Bit("角度3上限,角度3上限百分比,角度3下限,角度3下限百分比,角度3不确定,");
        strData += QString::fromLocal8Bit("角度4上限,角度4上限百分比,角度4下限,角度4下限百分比,角度4不确定");


		break;
	case 1:
		// 正方形
        // 宽度  长度   厚度  角度1 ~ 4
		strData += QString::fromLocal8Bit("宽度 OK,宽度 NG,长度 OK,长度 NG,厚度 OK,厚度 NG,");
		strData += QString::fromLocal8Bit("宽度上限,宽度上限百分比,宽度下限,宽度下限百分比,宽度不确定,");
		strData += QString::fromLocal8Bit("长度上限,长度上限百分比,长度下限,长度下限百分比,长度不确定,");
        strData += QString::fromLocal8Bit("厚度上限,厚度上限百分比,厚度下限,厚度下限百分比,厚度不确定,");


        strData += QString::fromLocal8Bit("角度1 OK,角度1 NG,角度2 OK,角度2 NG,角度3 OK,角度3 NG,角度4 OK,角度4 NG,");
        strData += QString::fromLocal8Bit("角度1上限,角度1上限百分比,角度1下限,角度1下限百分比,角度1不确定,");
        strData += QString::fromLocal8Bit("角度2上限,角度2上限百分比,角度2下限,角度2下限百分比,角度2不确定,");
        strData += QString::fromLocal8Bit("角度3上限,角度3上限百分比,角度3下限,角度3下限百分比,角度3不确定,");
        strData += QString::fromLocal8Bit("角度4上限,角度4上限百分比,角度4下限,角度4下限百分比,角度4不确定");
		break;
	case 2:
		// 梯形
		//0 高度   2 左斜边角度   3  右斜边角度
		strData += QString::fromLocal8Bit("高度 OK,高度 NG,左斜边角度 OK,左斜边角度 NG,右斜边角度 OK,右斜边角度 NG,");
		strData += QString::fromLocal8Bit("高度上限,高度上限百分比,高度下限,高度下限百分比,高度不确定,");
		strData += QString::fromLocal8Bit("左斜边上限,左斜边上限百分比,左斜边下限,左斜边下限百分比,左斜边不确定,");
		strData += QString::fromLocal8Bit("右斜边上限,右斜边上限百分比,右斜边下限,右斜边下限百分比,右斜边不确定");
		break;
	case 3:
		// 异形
		//高度  宽度 厚度
		strData += QString::fromLocal8Bit("高度 OK,高度 NG,宽度 OK,宽度 NG,厚度 OK,厚度 NG,");
		strData += QString::fromLocal8Bit("高度上限,高度上限百分比,高度下限,高度下限百分比,高度不确定,");
		strData += QString::fromLocal8Bit("宽度上限,宽度上限百分比,宽度下限,宽度下限百分比,宽度不确定,");
		strData += QString::fromLocal8Bit("厚度上限,厚度上限百分比,厚度下限,厚度下限百分比,厚度不确定");
		break;
	case 4:
		// 扇形
		//弧边1长度  弧边2长度  直边3长度  直边4长度  中直长度 轮廓度
		strData += QString::fromLocal8Bit("弧边1长度 OK,弧边1长度 NG,弧边2长度 OK,弧边2长度 NG,直边3长度 OK,直边3长度 NG,"
			"直边4长度OK,直边4长度 NG,中直长度 OK,中直长度 NG,轮廓度 OK,轮廓度 NG,");

		strData += QString::fromLocal8Bit("弧边1上限,弧边1上限百分比,弧边1下限,弧边1下限百分比,弧边1不确定,");
		strData += QString::fromLocal8Bit("弧边2上限,弧边2上限百分比,弧边2下限,弧边2下限百分比,弧边2不确定,");
		strData += QString::fromLocal8Bit("直边3上限,直边3上限百分比,直边3下限,直边3下限百分比,直边3不确定,");
		strData += QString::fromLocal8Bit("直边4上限,直边4上限百分比,直边4下限,直边4下限百分比,直边4不确定,");
		strData += QString::fromLocal8Bit("中直上限,中直上限百分比,中直下限,中直下限百分比,中直不确定,");
		strData += QString::fromLocal8Bit("轮廓度上限,轮廓度上限百分比,轮廓度下限,轮廓度下限百分比,轮廓度不确定");
		break;
	case 5:
		// 子弹头
		//宽度  长度  角度1  角度2
		strData += QString::fromLocal8Bit("宽度 OK,宽度 NG,长度 OK,长度 NG,角度1 OK,角度1 NG,角度2,角度2 NG,");
		strData += QString::fromLocal8Bit("宽度上限,宽度上限百分比,宽度下限,宽度下限百分比,宽度不确定,");
		strData += QString::fromLocal8Bit("长度上限,长度上限百分比,长度下限,长度下限百分比,长度不确定");
		break;
	default:
		break;
	}



	if (tempFile.open(QIODevice::ReadWrite | QIODevice::Text))
	{
		QTextStream out(&tempFile);    // 设置标题
		out << strData << "\n";
	}
	tempFile.close();
	//将程序当前路径设置为原来的路径
	tempDir.setCurrent(currentDir);
}
